package br.com.locadora.frame;

import br.com.locadora.classes.Categoria;
import br.com.locadora.classes.Persistencia;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author javaJacques
 */
public class CadCategorias extends javax.swing.JDialog {

     public static SessionFactory sfac = new Configuration().configure().buildSessionFactory();
    public static Session se = sfac.openSession();
    Transaction tx = se.beginTransaction();
    static int x2;
    
    
    
    
    public CadCategorias(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setUndecorated(true);
        initComponents();
        setLocationRelativeTo(null);
        limpaFormulario();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel12 = new javax.swing.JPanel();
        jLabel28 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnSalvar = new javax.swing.JButton();
        btnLimpar = new javax.swing.JButton();
        btnProcurar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnFechar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txtVlrCat = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNomecat = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel12.setBackground(javax.swing.UIManager.getDefaults().getColor("InternalFrame.activeTitleGradient"));
        jPanel12.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));

        jLabel28.setFont(new java.awt.Font("Arial", 1, 24));
        jLabel28.setText("Categoria");
        jPanel12.add(jLabel28);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        btnSalvar.setFont(new java.awt.Font("Arial", 1, 14));
        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/save_f2.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        jPanel1.add(btnSalvar);

        btnLimpar.setFont(new java.awt.Font("Arial", 1, 14));
        btnLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/ark_addfile.png"))); // NOI18N
        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });
        jPanel1.add(btnLimpar);

        btnProcurar.setFont(new java.awt.Font("Arial", 1, 14));
        btnProcurar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Lupa.png"))); // NOI18N
        btnProcurar.setText("Pesquisa");
        btnProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcurarActionPerformed(evt);
            }
        });
        jPanel1.add(btnProcurar);

        btnExcluir.setFont(new java.awt.Font("Arial", 1, 14));
        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cancel.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });
        jPanel1.add(btnExcluir);

        btnEditar.setFont(new java.awt.Font("Arial", 1, 14));
        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/ico_autocad2.gif"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        jPanel1.add(btnEditar);

        btnFechar.setFont(new java.awt.Font("Arial", 1, 14));
        btnFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/sair.png"))); // NOI18N
        btnFechar.setText("Sair");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });
        jPanel1.add(btnFechar);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        txtVlrCat.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtVlrCatKeyTyped(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("Nome Categoria");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("Valor Categoria");

        txtNomecat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomecatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(142, 142, 142)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNomecat, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                    .addComponent(txtVlrCat, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(144, 144, 144))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNomecat, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtVlrCat, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel12, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 224, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(66, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(84, 84, 84)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed


        try {
            
             if (txtNomecat.getText().trim().equals("")){   
            JOptionPane.showMessageDialog(this, "Por favor digite o nome da Categoria!");
          txtNomecat.requestFocus();
            
            return; }
       
            else if (txtVlrCat.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Por favor informe o valor da categoria");
           txtVlrCat.requestFocus();
            return; 
            }
            
            se = sfac.openSession();
            tx = se.beginTransaction();
            
            Categoria c = new Categoria();
            c.setNome(this.txtNomecat.getText());
            c.setPreco(Float.parseFloat(this.txtVlrCat.getText()));
            Persistencia.salvarObjeto(c);
            Persistencia.mensagem("Categoria", "Cadastrada com sucesso!");
        } catch (Exception e) {
        }
}//GEN-LAST:event_btnSalvarActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        this.txtNomecat.setText("");
        this.txtVlrCat.setText("");



}//GEN-LAST:event_btnLimparActionPerformed

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        int ret = JOptionPane.showConfirmDialog(this, "Deseja realmente sair do formulário?", "Sair",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE);

        if (ret == JOptionPane.OK_OPTION) {
            this.dispose();
        }
}//GEN-LAST:event_btnFecharActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
try{
        
        if (txtNomecat.getText().trim().equals("")){   
            JOptionPane.showMessageDialog(this, "Por favor digite o nome da Categoria!");
          txtNomecat.requestFocus();
            
            return; }
       
            else if (txtVlrCat.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Por favor informe o valor da categoria");
           txtVlrCat.requestFocus();
            return; 
            }
            
            se = sfac.openSession();
            tx = se.beginTransaction();
            
            Categoria c = new Categoria();
            c.setIdCategoriaMidia((x2));
            c.setNome(this.txtNomecat.getText());
            c.setPreco(Float.parseFloat(this.txtVlrCat.getText()));
            se.update(c);
             tx.commit();
             se.close();
            Persistencia.mensagem("Categoria", "Cadastrada com sucesso!");
        } catch (Exception e) {
            
            Persistencia.mensagem("Ta bravo ","deu erro ");
        }
        
               limpaFormulario();



}//GEN-LAST:event_btnEditarActionPerformed

    private void btnProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcurarActionPerformed

        DlgConsultaCat con = new DlgConsultaCat(null, rootPaneCheckingEnabled);
        con.setVisible(true);

    }//GEN-LAST:event_btnProcurarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void txtVlrCatKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtVlrCatKeyTyped
        // TODO add your handling code here:
        
        int k = evt.getKeyChar();  
         if((k > 45 && k < 58)) {  
                 if(txtVlrCat.getText().length() == 8){  
                 evt.setKeyChar((char)KeyEvent.VK_CLEAR);  
         }  
         } else {  
             evt.setKeyChar((char)KeyEvent.VK_CLEAR);  
         }    
        
        
    }//GEN-LAST:event_txtVlrCatKeyTyped

    private void txtNomecatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomecatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomecatActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CadCategorias dialog = new CadCategorias(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
//  public  int recebe(int x){}          
//            
//            
//    public  int receber(int x) {
//        se = sfac.openSession();
//        Categoria c = new Categoria();
//        c = (Categoria) se.load(Categoria.class, new Integer((x)));  
//        txtNomecat.setName(c.getNome());
//        
////        txtGrupo.setText(c.getGrupoCanto());
////        txtIdade.setText(Integer.toString(c.getIdadeLocacao()));        
////        txtQtd.setText(Integer.toString(c.getQuantidadeMusica()));
////        cboCategoria.setSelectedItem(c.getCategoria());
////        txtTitulo.setText(c.getTitulo());
//        btnSalvar.setVisible(false);
//       btnExcluir.setVisible(true);
//        btnEditar.setVisible(true);
//        se.close();
//        return x2 = x;
//    }
            
        });
        
        
        
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnFechar;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnProcurar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel2;
    public javax.swing.JTextField txtNomecat;
    private javax.swing.JTextField txtVlrCat;
    // End of variables declaration//GEN-END:variables

    public final void limpaFormulario() {
        btnSalvar.setVisible(true);
        txtVlrCat.setText("");

        txtNomecat.setText("");

    }
}
